package cn.myspringboot.system.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.myspringboot.base.service.impl.BaseServiceImpl;
import cn.myspringboot.system.mapper.LogininfoMapper;
import cn.myspringboot.system.mapper.MenuMapper;
import cn.myspringboot.system.service.LogininfoService;
import cn.myspringboot.user.config.JwtUtil;
import cn.myspringboot.user.config.Payload;
import cn.myspringboot.user.domain.Employee;
import cn.myspringboot.user.domain.Logininfo;
import cn.myspringboot.user.domain.Menu;
import cn.myspringboot.user.domain.vo.LoginVo;
import cn.myspringboot.utils.R;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LogininfoServiceImpl extends BaseServiceImpl<Logininfo> implements LogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public R login(LoginVo loginVo) {
        //校验用户名与密码是否输入
        if(StringUtil.isEmpty(loginVo.getUsername())){
            return R.fail("请输入用户名");
        }
        if(StringUtil.isEmpty(loginVo.getPassword())){
            return R.fail("请输入密码");
        }
        //根据用户名与类型判断是否存在  登录信息前端传回来的数据
        Logininfo logininfo = logininfoMapper.fingLogininfoByUserNameAndType(loginVo);
        if(logininfo == null){
            return R.fail("账号或密码错误");
        }
        //查询数据库的密码与盐值
        String password = loginVo.getPassword();
        String salt = logininfo.getSalt();
        //将查询出来的数据加密再拼接
        String pwdMd = SecureUtil.md5(salt + password);
        //和前端传回来的密码做对比
        if (!logininfo.getPassword().equals(pwdMd)) {
            return R.fail("账号或密码错误");
        }
        //如果一样就生成token、用户信息、菜单信息、权限信息返回给前端 前端保存
        //创建一个payload存储用户信息
        Payload payload = new Payload();
        //将用户信息存储到payload中
        payload.setLogininfo(logininfo);
        //生成一个token
        String token = JwtUtil.generateToken(payload);
        //获取菜单信息和权限信息 并用map存储
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        //判断是否为管理员
        if(logininfo.getType() == 0){
            //前端传回来的id
            Long id = logininfo.getId();
            //第一根据logininfo找到员工id 就可以获取用户信息
            Employee employee = logininfoMapper.fingEmployeeByLogininfo(id);
            map.put("employeeInfo", employee);
            payload.setEmployee(employee);
            //根据员工id查找菜单id 就可以获取菜单信息
            List<Menu> menus = logininfoMapper.fingMenuByEmployeeId(employee.getId());
//            map.put("menus", menus);
//            payload.setMenus(menus);
            //存储自己的id并去重
            List<Long> menupid = menus.stream()
                    .map(Menu::getParentId).distinct()
                    .collect(Collectors.toList());
            //查找父级id
            List<Menu> menusP = menuMapper.findIds(menupid);
            //存储所有的父级id
            menus.addAll(menusP);
            //将菜单显示为层级
            List<Menu> menusTree = MenuServiceImpl.getTree(menus);
            payload.setMenus(menusTree);
            map.put("menus",menusTree);
            //根据员工id查找权限id 就可以获取权限信息
            List<String> permissions = logininfoMapper.fingPerByEmployeeId(employee.getId());
            payload.setPermissions(permissions);
            map.put("permissions", permissions);
        }
        return R.ok(map);
    }
}
